package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.ame.v20190916.models.Album;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")

public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;

    /**
     * 保存专辑
     *
     * @param albumInfoVo
     * @return
     */
    @TsLogin
    @Operation(summary = "新增专辑")
    @PostMapping("saveAlbumInfo")
    public Result save(@RequestBody AlbumInfoVo albumInfoVo) {
        albumInfoService.saveAlbumInfo(albumInfoVo, AuthContextHolder.getUserId());
        return Result.ok();

    }

    @TsLogin
    @Operation(summary = "获取当前用户专辑分页列表")
    @PostMapping("findUserAlbumPage/{page}/{limit}")
    public Result findUserAlbumPage(@Parameter(name = "page", description = "当前页码", required = true)
                                    @PathVariable Long page,
                                    @Parameter(name = "limit", description = "每页记录数", required = true)
                                    @PathVariable Long limit,
                                    @Parameter(name = "albumInfoQuery", description = "查询对象", required = false)
                                    @RequestBody AlbumInfoQuery albumInfoQuery) {
        albumInfoQuery.setUserId(AuthContextHolder.getUserId());
        Page<AlbumListVo> albumInfoPage = new Page<>(page, limit);
        //调用服务层方法
        IPage<AlbumListVo> ipage = this.albumInfoService.findUserAlbumPage(albumInfoPage, albumInfoQuery);

        return Result.ok(ipage);
    }


    @Operation(summary = "根据id删除专辑信息")
    @DeleteMapping("removeAlbumInfo/{id}")
    public Result removeAlbumInfo(@PathVariable Long id) {
        albumInfoService.removeAlbumInfoById(id);
        return Result.ok();
    }

    @Operation(summary = "获取专辑信息")
    @GetMapping("getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfoById(@PathVariable Long id) {
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(id);
        return Result.ok(albumInfo);
    }

    @Operation(summary = "修改专辑")
    @PutMapping("/updateAlbumInfo/{id}")
    public Result updateAlbumInfo(@PathVariable Long id, @RequestBody @Validated AlbumInfoVo albumInfoVo) {
        albumInfoService.updateAlbumInfo(id, albumInfoVo);
        return Result.ok();
    }


    @Operation(summary = "获取当前用户全部专辑列表")
    @GetMapping("findUserAllAlbumList")
    public Result findUserAllAlbumList() {
        Long userId = null == AuthContextHolder.getUserId() ? 1l : AuthContextHolder.getUserId();
        List<AlbumInfo> list = albumInfoService.findUserAlbumList(userId);
        return Result.ok(list);
    }

    /**
     * 根据专辑id获取到专辑属性列表
     *
     * @param albumId
     * @return
     */
    @Operation(summary = "获取专辑属性值列表")
    @GetMapping("findAlbumAttributeValue/{albumId}")
    public Result<List<AlbumAttributeValue>> findAlbumAttributeValue(@PathVariable Long albumId) {
        //获取到专辑属性集合
        List<AlbumAttributeValue> albumAttributeValueList = albumInfoService.findAlbumAttributeValueByAlbumId(albumId);
        return Result.ok(albumAttributeValueList);
    }

    @Operation(summary = "获取统计信息")
    @GetMapping("/getAlbumStatVo/{albumId}")
    public Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId) {
        //调用服务层方法
        AlbumStatVo albumStatVo = albumInfoService.getAlbumStatVo(albumId);
        //返回数据
        return Result.ok(albumStatVo);
    }
}






















